<template>
  <el-dialog :title="title" :visible="showDialog" @close="close">
    <el-form ref="deptForm" label-width="120px" :model="deptForm" :rules="rules">
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="deptForm.name" placeholder="2-10个字符" size="mini" style="width: 80%;">1</el-input>
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input v-model="deptForm.code" placeholder="2-10个字符" size="mini" style="width: 80%;">2</el-input>
      </el-form-item>
      <el-form-item label="部门负责人" prop="managerId">
        <el-select v-model="deptForm.managerId" placeholder="请选择部门负责人">
          <el-option
            v-for="item in managerList"
            :key="item.id"
            :label="item.username"
            :value="item.id"
          /></el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input
          v-model="deptForm.introduce"
          type="textarea"
          :rows="4"
          placeholder="1-100个字符"
          size="mini"
          style="width: 80%;"
        >4</el-input>
      </el-form-item>
      <el-form-item>
        <el-row type="flex" style="width: 80%;" justify="center">
          <el-button size="mini" type="primary" @click="submitDept">确定</el-button>
          <el-button size="mini" @click="close">取消</el-button>
        </el-row>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script>
// import { DeptaddAPI } from '@/api/user'
import { departmentDataAPI,
  getDepartmentsAPI,
  DeptaddAPI,
  getDepartmentDetailAPI,
  updateDepartmentAPI } from '@/api/department'
export default {
  props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    currentDepeptId: {
      type: Number,
      default: null
    }
  },
  data() {
    return {
      deptForm: {
        name: '',
        code: '',
        managerId: '',
        introduce: '',
        pid: ''
      },
      managerList: [],
      rules: {
        name: [
          { required: true, message: '请输入部门名称', trigger: 'blur' },
          { min: 2, max: 10, message: '部门名称是2-10个字符', trigger: 'blur' },
          { validator: async(rule, value, callback) => {
            let data = await departmentDataAPI()
            if (this.deptForm.id) {
              data = data.filter(item => item.id !== this.deptForm.id)
            }
            const isOk = data.some(item => item.name === value)
            isOk ? callback(new Error(`部门名称与${value}重复`)) : callback()
          }, trigger: 'blur' }
        ],
        code: [
          { required: true, message: '请输入部门编号', trigger: 'blur' },
          { min: 2, max: 10, message: '部门编码是2-10个字符', trigger: 'blur' },
          { validator: async(rule, value, callback) => {
            let data = await departmentDataAPI()
            if (this.deptForm.id) {
              data = data.filter(item => item.id !== this.deptForm.id)
            }
            const isOk = data.some(item => item.code === value)
            isOk ? callback(new Error(`部门编码与${value}重复`)) : callback()
          }, trigger: 'blur' }
        ],
        managerId: [{ required: true, message: '请输入部门负责人', trigger: 'blur' }],
        introduce: [{ required: true, message: '请输入部门介绍', trigger: 'blur' },
          { min: 1, max: 100, message: '部门介绍是2-10个字符', trigger: 'blur' }]
      }
    }
  },
  computed: {
    title() {
      return this.deptForm.id ? '编辑部门' : '新增部门'
    }
  },
  created() {
    this.getDepartments()
  },
  methods: {
    close() {
      this.deptForm = {
        name: '',
        code: '',
        managerId: '',
        introduce: '',
        pid: ''
      }
      this.$refs.deptForm.resetFields()
      this.$emit('update:showDialog', false)
    },
    async getDepartments() {
      this.managerList = await getDepartmentsAPI()
    },
    submitDept() {
      this.$refs.deptForm.validate(async(isOk) => {
        if (isOk) {
          if (this.deptForm.id) {
            await updateDepartmentAPI(this.deptForm)
            this.$message.success('修改部门成功')
          } else {
            await DeptaddAPI({ ...this.deptForm, pid: this.currentDepeptId })
            this.$message.success('新增部门成功')
          }
          this.$emit('updateDepartmentList')
          this.close()
        }
      })
    },
    async getDepartmentDetail() {
      const res = await getDepartmentDetailAPI(this.currentDepeptId)
      this.deptForm = res
    }
  }
}
</script>

<style>

</style>
